<?php
class Administrator_NewsController extends Nguyen_Controller_Action{
	public function init(){
		$this->loadTemplate('administrator');
	}
	public function indexAction(){ 
		
	}	
	public function sectionAction(){ 
		$model = new Administrator_Model_News_View();
		$task = $this->_request->getParam('task','list');
		$id = $this->_request->getParam('id');
		$this->view->task = $task;
		$this->view->id = $id;
		if ($this->_request->isPost()){	
		if ($this->_request->getParam('btadd')){
				$this->_redirect('/administrator/news/section?task=new');
			}
			if ($this->_request->getParam('btsave')){
				$namevi = $this->_request->getParam('namevi','');
				$nameen = $this->_request->getParam('nameen','');
				$keyword = $this->_request->getParam('keyword','');
				$description = $this->_request->getParam('description','');
				if($task == 'new'){
					$name = $model->InsertLangContent($namevi, $nameen);
					$result = $model->insertSection($name, $keyword, $description, '1');
				}
				if($task == 'edit'){
					$info = $model->GetSectionInfo($id);
					$model->UpdateLangContent($info['name'], $namevi, $nameen);
					$model->updateSection($id, $keyword, $description);
				}
				$this->_redirect('/administrator/news/section');
			}
			if ($this->_request->getParam('btedit')){
				foreach ($model->GetAllSection() as $value) {
					if($this->_request->getParam($value['id']))
						$this->_redirect('/administrator/news/section?task=edit&id='.$value['id']);
				}
			}
			if ($this->_request->getParam('btdelete')){
				foreach ($model->GetAllSection() as $value) {
					if($this->_request->getParam($value['id']))
						$model->deleteSection($value['id']);
				}
				$this->_redirect('/administrator/news/section');
			}
			if ($this->_request->getParam('btback')){
				$this->_redirect('/administrator/news/section');
			}
			if ($this->_request->getParam('btoption')){
				$this->_redirect('/administrator');
			}
		}
		$this->view->headTitle('Control Panel | Thông tin thể loại',true);
	}	
	public function catalogueAction(){ 
		$model = new Administrator_Model_News_View();
		$task = $this->_request->getParam('task','list');
		$id = $this->_request->getParam('id');
		$this->view->task = $task;
		$this->view->id = $id;
		if ($this->_request->isPost()){	
		if ($this->_request->getParam('btadd')){
				$this->_redirect('/administrator/news/catalogue?task=new');
			}
			if ($this->_request->getParam('btsave')){
				$namevi = $this->_request->getParam('namevi','');
				$nameen = $this->_request->getParam('nameen','');
				$keyword = $this->_request->getParam('keyword','');
				$description = $this->_request->getParam('description','');
				$introvi = $this->_request->getParam('introvi','');
				$introen = $this->_request->getParam('introen','');
				$section = $this->_request->getParam('section');
				$showrelated = 1;if($this->_request->getParam('showrelated')) $showrelated = 0;
				$showdate = 1;if($this->_request->getParam('showdate')) $showdate = 0;
				$showcat = 1;if($this->_request->getParam('showcat')) $showcat = 0;
				$showsec = 1;if($this->_request->getParam('showsec')) $showsec = 0;
				$showtag = 1;if($this->_request->getParam('showtag')) $showtag = 0;
				$showhit = 1;if($this->_request->getParam('showhit')) $showhit = 0;
				$showuser = 1;if($this->_request->getParam('showuser')) $showuser = 0;
				$showsocial = 1;if($this->_request->getParam('showsocial')) $showsocial = 0;
				$key = 1;if($this->_request->getParam('key')) $key = 0;
				$view = $this->_request->getParam('view','0');
				if($task == 'new'){
					$f = $_FILES['image'];
					$file_temp = $f['tmp_name'];
					$filename = $f['name'];					
					$imgname = md5($model->curentday()).$filename;
					if(move_uploaded_file($file_temp, PUBLIC_PATH.'/images/news/'.$imgname)){
						
					}
					else{
						$imgname = '';
					}
					if($section){
						$name = $model->InsertLangContent($namevi, $nameen);
						$intro = $model->InsertLangContent($introvi, $introen);
						$result1 = $model->insertCatalogueEnable($name, $intro, $imgname, $keyword, $description, '0', $section, $showrelated, $showdate, $showsec, $showcat, $showtag, $showhit, $showuser, $showsocial, $key, $view, '1');
					}
				}
				if($task == 'edit'){
					$info = $model->GetCatalogueInfo($id);
					$f = $_FILES['image'];
					$file_temp = $f['tmp_name'];
					$filename = $f['name'];
					$imgname = md5($model->curentday()).$filename;
					if(move_uploaded_file($file_temp, PUBLIC_PATH.'/images/news/'.$imgname)){
						$model->DeleteImageOnHost($info['image'], 'news');
					}
					else{
						$imgname = $info['image'];
					}
					$model->UpdateLangContent($info['name'], $namevi, $nameen);
					$model->UpdateLangContent($info['intro'], $introvi, $introen);
					$model->updateCatalogueEnable($id, $imgname, $keyword, $description, '0', $section, $showrelated, $showdate, $showsec, $showcat, $showtag, $showhit, $showuser, $showsocial, $key,$view);
				}
				$this->_redirect('/administrator/news/catalogue');
			}
			if ($this->_request->getParam('btback')){
				$this->_redirect('/administrator/news/catalogue');
			}
			if ($this->_request->getParam('btedit')){
				foreach ($model->GetAllCatalogue() as $value) {
					if($this->_request->getParam($value['id']))
						$this->_redirect('/administrator/news/catalogue?task=edit&id='.$value['id']);
				}
			}
			if ($this->_request->getParam('btdelete')){
				foreach ($model->GetAllCatalogue() as $value) {
					if($this->_request->getParam($value['id']))
						$model->deleteCatalogue($value['id']);
				}
				$this->_redirect('/administrator/news/catalogue');
			}
			if ($this->_request->getParam('btoption')){
				$this->_redirect('/administrator');
			}
		}
		$this->view->headTitle('Control Panel | Thông tin danh mục',true);
	}	
	public function contentAction(){ 
		$auth = Zend_Auth::getInstance();
		$infoUser = $auth->getIdentity();
		$user = '';
		if($auth->hasIdentity()){
			$user = $infoUser->id;
		}
		$model = new Administrator_Model_News_View();
		$task = $this->_request->getParam('task','list');
		$id = $this->_request->getParam('id');
		$this->view->task = $task;
		$this->view->id = $id;
		if ($this->_request->isPost()){	
		if ($this->_request->getParam('btadd')){
				$this->_redirect('/administrator/news/content?task=new');
			}
			if ($this->_request->getParam('btsave')){
				$namevi = $this->_request->getParam('namevi','');
				$nameen = $this->_request->getParam('nameen','');
				$tag = $this->_request->getParam('tag','');
				$keyword = $this->_request->getParam('keyword','');
				$description = $this->_request->getParam('description','');
				$introvi = $this->_request->getParam('introvi','');
				$introen = $this->_request->getParam('introen','');
				$contentvi = $this->_request->getParam('contentvi','');
				$contenten = $this->_request->getParam('contenten','');
				$parent = $this->_request->getParam('parent');
				$showrelated = 1;if($this->_request->getParam('showrelated')) $showrelated = 0;
				$showdate = 1;if($this->_request->getParam('showdate')) $showdate = 0;
				$showcat = 1;if($this->_request->getParam('showcat')) $showcat = 0;
				$showsec = 1;if($this->_request->getParam('showsec')) $showsec = 0;
				$showtag = 1;if($this->_request->getParam('showtag')) $showtag = 0;
				$showhit = 1;if($this->_request->getParam('showhit')) $showhit = 0;
				$showuser = 1;if($this->_request->getParam('showuser')) $showuser = 0;
				$showsocial = 1;if($this->_request->getParam('showsocial')) $showsocial = 0;
				$key = 1;if($this->_request->getParam('key')) $key = 0;
				$view = $this->_request->getParam('view','0');
				if($task == 'new'){$f = $_FILES['image'];
					$file_temp = $f['tmp_name'];
					$filename = $f['name'];					
					$imgname = md5($model->curentday()).$filename;
					if(move_uploaded_file($file_temp, PUBLIC_PATH.'/images/news/'.$imgname)){
						
					}
					else{
						$imgname = '';
					}
					if($parent){
						$name = $model->InsertLangContent($namevi, $nameen);
						$intro = $model->InsertLangContent($introvi, $introen);
						$content = $model->InsertLangContent($contentvi, $contenten);
						$result = $model->insertContentEnable($name, $imgname, $intro, $content, $tag, $keyword, $description, $parent, $model->curentday(), '0', $user, $showrelated, $showdate, $showsec, $showcat, $showtag, $showhit, $showuser, $showsocial, $key, $view, '1');
					}
				}
				if($task == 'edit'){
					$info = $model->GetContentInfo($id);
					$f = $_FILES['image'];
					$file_temp = $f['tmp_name'];
					$filename = $f['name'];
					$imgname = md5($model->curentday()).$filename;
					if(move_uploaded_file($file_temp, PUBLIC_PATH.'/images/news/'.$imgname)){
						$model->DeleteImageOnHost($info['image'], 'news');
					}
					else{
						$imgname = $info['image'];
					}
					$model->UpdateLangContent($info['name'], $namevi, $nameen);
					$model->UpdateLangContent($info['intro'], $introvi, $introen);
					$model->UpdateLangContent($info['content'], $contentvi, $contenten);
					$model->updateContentEnanle($id, $imgname, $tag, $keyword, $description, $parent, $showrelated, $showdate, $showsec, $showcat, $showtag, $showhit, $showuser, $showsocial, $key,$view);
				}
				$this->_redirect('/administrator/news/content');
			}
			if ($this->_request->getParam('btback')){
				$this->_redirect('/administrator/news/content');
			}
			if ($this->_request->getParam('btedit')){
				foreach ($model->GetAllContent() as $value) {
					if($this->_request->getParam($value['id']))
						$this->_redirect('/administrator/news/content?task=edit&id='.$value['id']);
				}
			}
			if ($this->_request->getParam('btdelete')){
				foreach ($model->GetAllContent() as $value) {
					if($this->_request->getParam($value['id']))
						$model->deleteContent($value['id']);
				}
				$this->_redirect('/administrator/news/content');
			}
			if ($this->_request->getParam('btoption')){
				$this->_redirect('/administrator');
			}
		}
		$this->view->headTitle('Control Panel | Thông tin bài viết',true);
	}
	public function rendercatbysectionAction(){
		$this->_helper->layout()->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender(true);
        $mview = new Administrator_Model_News_View();
		$section = $this->_request->getParam('sectionid');
		$info = $mview->GetSiteInfo();
		$listcat = $mview->GetAllCatBySection($section);
		foreach ($listcat as $value) {
			$title = $mview->TranslateByLang($value['name'], 'vi');
			if($info['status']==1)$title = $this->TranslateByLang($value['name'], 'vi').' - '.$this->TranslateByLang($value['name'], 'en');
			echo '<option value="'.$value['id'].'">'.$title.'</option>';;
		}
	}
	public function catalogueshowrelatedAction(){
		$this->_helper->layout()->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender(true);
        $mview = new Administrator_Model_News_View();
        $contentid = $this->_request->getParam('catid');
        $info = $mview->GetCatalogueInfo($contentid);
        if($info['showrelated'] == '0'){$mview->updateCatRelated($contentid, '1');}
        else{$mview->updateCatRelated($contentid, '0');}
	}
	public function catalogueshowdateAction(){
		$this->_helper->layout()->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender(true);
        $mview = new Administrator_Model_News_View();
        $contentid = $this->_request->getParam('catid');
        $info = $mview->GetCatalogueInfo($contentid);
        if($info['showdate'] == '0'){$mview->updateCatdate($contentid, '1');}
        else{$mview->updateCatdate($contentid, '0');}
	}
	public function catalogueshowcatAction(){
		$this->_helper->layout()->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender(true);
        $mview = new Administrator_Model_News_View();
        $contentid = $this->_request->getParam('catid');
        $info = $mview->GetCatalogueInfo($contentid);
        if($info['showcat'] == '0'){$mview->updateCatcat($contentid, '1');}
        else{$mview->updateCatcat($contentid, '0');}
	}
	public function catalogueshowsecAction(){
		$this->_helper->layout()->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender(true);
        $mview = new Administrator_Model_News_View();
        $contentid = $this->_request->getParam('catid');
        $info = $mview->GetCatalogueInfo($contentid);
        if($info['showsec'] == '0'){$mview->updateCatsec($contentid, '1');}
        else{$mview->updateCatsec($contentid, '0');}
	}
	public function catalogueshowtagAction(){
		$this->_helper->layout()->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender(true);
        $mview = new Administrator_Model_News_View();
        $contentid = $this->_request->getParam('catid');
        $info = $mview->GetCatalogueInfo($contentid);
        if($info['showtag'] == '0'){$mview->updateCattag($contentid, '1');}
        else{$mview->updateCattag($contentid, '0');}
	}
	public function catalogueshowhitAction(){
		$this->_helper->layout()->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender(true);
        $mview = new Administrator_Model_News_View();
        $contentid = $this->_request->getParam('catid');
        $info = $mview->GetCatalogueInfo($contentid);
        if($info['showhit'] == '0'){$mview->updateCathit($contentid, '1');}
        else{$mview->updateCathit($contentid, '0');}
	}
	public function catalogueshowuserAction(){
		$this->_helper->layout()->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender(true);
        $mview = new Administrator_Model_News_View();
        $contentid = $this->_request->getParam('catid');
        $info = $mview->GetCatalogueInfo($contentid);
        if($info['showuser'] == '0'){$mview->updateCatuser($contentid, '1');}
        else{$mview->updateCatuser($contentid, '0');}
	}
	public function catalogueshowsocialAction(){
		$this->_helper->layout()->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender(true);
        $mview = new Administrator_Model_News_View();
        $contentid = $this->_request->getParam('catid');
        $info = $mview->GetCatalogueInfo($contentid);
        if($info['showsocial'] == '0'){$mview->updateCatsocial($contentid, '1');}
        else{$mview->updateCatsocial($contentid, '0');}
	}
	public function catalogueshowkeyAction(){
		$this->_helper->layout()->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender(true);
        $mview = new Administrator_Model_News_View();
        $contentid = $this->_request->getParam('catid');
        $info = $mview->GetCatalogueInfo($contentid);
        if($info['key'] == '0'){$mview->updateCatkey($contentid, '1');}
        else{$mview->updateCatkey($contentid, '0');}
	}
}